package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.HouseBroker;
import com.atguigu.service.AdminService;
import com.atguigu.service.HouseBrokerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Author: 翟文海
 * @Date: 2022/10/29/029 19:25
 * @Version: 1.0
 * @Description:
 **/
@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController extends BaseController {
    private static final String PAGE_CREATE = "houseBroker/create";
    private static final String PAGE_SUCCESS = "common/successPage";
    private static final String PAGE_EDIT = "houseBroker/edit";
    private static final String LIST_HOUSE = "redirect:/house";


    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private AdminService adminService;

    //opt.openWin('/houseBroker/create?houseId=[[${house.id}]]','新增经纪人',630,300)
    @GetMapping("/create")
    public String create(Long houseId, Model model) {

        List<Admin> adminList = adminService.findAdminListNotAssignHouseId(houseId);

        model.addAttribute("adminList", adminList);
        model.addAttribute("houseId", houseId);
        return toPage(PAGE_CREATE);
    }

    @PostMapping("/save")
    public String save(HouseBroker houseBroker, Model model) {
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        houseBrokerService.insert(houseBroker);
        return toPage(model, "新增经纪人成功", PAGE_SUCCESS);
    }

    /**
     * @param id    房屋经纪人的id
     * @param model
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        //列出经纪人列表:不包含当前房源已关联的那些admin(除了当前id表示的这条经纪人数据)
        HouseBroker houseBroker = houseBrokerService.getById(id);
        //房屋id
        Long houseId = houseBroker.getHouseId();
        //把不是当前房源的所有经纪人找到
        List<Admin> adminList = adminService.findAdminListNotAssignHouseId(houseId);
        //把当前的经纪人加入到不是当前房源的所有经纪列表
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        adminList.add(admin);
        model.addAttribute("adminList", adminList);
        model.addAttribute("houseBroker", houseBroker);
        return toPage(PAGE_EDIT);
    }

    @PostMapping("/update")
    public String update(HouseBroker houseBroker, Model model) {
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        if (admin != null) {
            houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
            houseBroker.setBrokerName(admin.getName());
        }
        houseBrokerService.update(houseBroker);
        return toPage(model, "修改经纪人成功！", PAGE_SUCCESS);
    }

    @GetMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable("houseId") Long houseId, @PathVariable("id") Long id) {
        houseBrokerService.delete(id);
        return toPage(LIST_HOUSE+"/"+houseId);
    }

}
